<template>
  <div class="login_container">
    <div class="login_box">
      <div class="login_img"><img src="../assets/logo.png" alt="" /></div>

      <el-form :model="loginForm" :rules="loginFormRules" ref="loginRuleForm">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-button type="primary" round class="btn_1" @click="loginIn"
          >登录</el-button
        >
        <el-button round class="btn_2" @click="resetFrom">重置</el-button>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login_container {
  display: flex;
  width: 100%;
  height: 100%;
  background-color: skyblue;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.login_box {
  position: relative;
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 15px;
  box-shadow: 0 0 10px #eee;
}

.login_img {
  position: absolute;
  width: 130px;
  height: 130px;
  left: 50%;
  margin-left: -65px;
  top: -57px;
  border-radius: 50%;
  box-shadow: 0 0 10px red;
  padding: 10px;
  background-color: #eee;
}

.login_img img {
  width: 100%;
  border-radius: 50%;
  border: 1px solid gold;
}

.el-form {
  position: absolute;
  margin-top: 128px;
  left: 50%;
  margin-left: -101px;
}

.btn_1 {
  margin-left: 10%;
}

.btn_2 {
  margin-left: 5%;
}
</style>

<script>
export default {
  data() {
    return {
      //这是登录绑定的数据对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "用户名长度在 3 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 16,
            message: "密码长度在3到16个字符之间",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //登录

    //async 要放在紧挨着await的方法前面
    loginIn() {
      this.$refs.loginRuleForm.validate(async (vaild) => {
        if (!vaild) return;
        //发起登录请求
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status === 200) {
          this.$message.success("成功登录");
          /*1. 将登陆成功之后的 token， 保存到客户端的session Storage 中
              1.1 项目中除了登录之外的其他API接口，必须在登录只够才能访问
              1.2 token 只应在当前网站打开期间生效，所以讲 token 保存在 sessionStorage 中
            2. 通过编程式当行跳转到后台主页，路由地址是/home
          */

          window.sessionStorage.setItem("token", res.data.token);
          this.$router.push("/home"); //跳转到home.vue
        } else {
          this.$message.error("登录失败");
        }
      });
    },
    //点击重置按钮
    resetFrom() {
      this.$refs.loginRuleForm.resetFields(); //直接调用函数方法
    },
  },
};
</script>